package com.humanminoritygame.model;

import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class HibernateQuestionRepo extends HibernateGenericRepo implements QuestionRepo{
  @Autowired
  public HibernateQuestionRepo(SessionFactory sessionFactory) {
    setSessionFactory(sessionFactory);
  }

  @Override
  public void addQuestion(Question question) {
    Session session = getSession();
    session.save(question);
    session.flush();
  }

  @SuppressWarnings("unchecked")
  @Override
  public Set<Question> getAllQuestions() {
    Set<Question> questions = new HashSet<Question>();
    questions.addAll(getSession().createQuery("SELECT q FROM Question q ORDER BY q.question").list());
    return questions;
  }

  @Override
  public Question getQuestionById(Long id) {
    return (Question)getSession().get(Question.class, id);
  }

}
